<?php
// +----------------------------------------------------------------------
// | HkCms 权限
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2019 http://www.hkcms.cn, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 广州恒企教育科技有限公司 <admin@hkcms.cn>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use libs\util\Tree;
use common\controller\AdminBase;

class Rbac extends AdminBase
{
    //角色管理首页
    public function rolemanage() 
    {

        $tree = new Tree();

        $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');

        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';

        $roleList = model("admin/Role")->getTreeArray();

        foreach ($roleList as $k => $rs) {

            $operating = '';

            if ($rs['id'] == 1) {

                $operating = '<font color="#cccccc">权限设置</font> | <a href="' . url('Management/manager', array('role_id' => $rs['id'])) . '">成员管理</a> | <font color="#cccccc">修改</font> | <font color="#cccccc">删除</font>';

            } else {

                $operating = '<a href="' . url("Rbac/authorize", array("id" => $rs["id"])) . '">权限设置</a> | <a href="' . url("Rbac/setting_cat_priv", array("roleid" => $rs["id"])) . '">栏目权限</a> | <a href="' . url('Management/manager', array('role_id' => $rs['id'])) . '">成员管理</a> | <a href="' . url('Rbac/roleedit', array('id' => $rs['id'])) . '">修改</a> | <a class="J_ajax_del" href="' . url('Rbac/roledelete', array('id' => $rs['id'])) . '">删除</a>';
            }

            $roleList[$k]['operating'] = $operating;
        }

        $str = "<tr>
          <td>\$id</td>
          <td>\$spacer\$name</td>
          <td>\$remark</td>
          <td align='center'><font color='red'>√</font></td>
          <td align='center'>\$operating</td>
        </tr>";

        $tree->init($roleList);

        $this->assign("role", $tree->get_tree(0, $str));

        $this->assign("data", model("admin/Role")->order(array("listorder" => "asc", "id" => "desc"))->select()->toarray());

        return $this->fetch();
    }

    //添加角色
    public function roleadd() {
        
        if ( request()->isPost() ) {

            $data = [
                
                'parentid' => input('parentid'),

                'name' => input('name'),

                'remark' => input('remark'),

                'status' => input('status'),
            ];

            if (model("admin/Role")->data($data)->save()) {

                $this->success("添加角色成功！", url("Rbac/rolemanage"));

            } 

            $this->error("添加失败！");
        }

        return $this->fetch();
    }

    //删除角色
    public function roledelete() 
    {

        $id = input('id', 0, 'intval');

        if (model("admin/Role")->roleDelete($id)) {

            $this->success("删除成功！", url('Rbac/rolemanage'));

        } else {

            $error = model("admin/Role")->getError();

            $this->error($error ? $error : '删除失败！');
        }
    }

    //编辑角色
    public function roleedit() 
    {
        if ( request()->isPost() ) {

            if (model("admin/Role")->update(input('post.'))) {
                
                $this->success("修改成功！", url('Rbac/rolemanage'));

            }
                
            $this->error("修改失败！");
        } 

        $id = input('id', 0, 'intval');

        if (empty($id)) {

            $this->error('请选择需要编辑的角色！');
        }

        if (1 == $id) {

            $this->error("超级管理员角色不能被修改！");
        }

        $data = model("admin/Role")->where("id",$id)->find();

        if (empty($data)) {

            $this->error("该角色不存在！", url('rolemanage'));
        }

        $this->assign("data", $data);

        return $this->fetch();
    }

    //角色授权
    public function authorize() 
    {
        if ( request()->isPost() ) {

            $roleid = input('post.roleid', 0, 'intval');

            if (!$roleid) {

                $this->error("需要授权的角色不存在！");
            }

            //被选中的菜单项
            $menuidAll = explode(',', input('post.menuid', ''));

            if (is_array($menuidAll) && count($menuidAll) > 0) {

                //取得菜单数据
                $menu_info = hk_cache('Menu');

                $addauthorize = array();

                //检测数据合法性
                foreach ($menuidAll as $menuid) {

                    if (empty($menu_info[$menuid])) {

                        continue;
                    }

                    $info = [

                        'app' => $menu_info[$menuid]['app'],

                        'controller' => $menu_info[$menuid]['controller'],

                        'action' => $menu_info[$menuid]['action'],

                        'type' => $menu_info[$menuid]['type'],
                    ];

                    //菜单项
                    if ($info['type'] == 0) {

                        $info['app'] = $info['app'];

                        $info['controller'] = $info['controller'] . $menuid;

                        $info['action'] = $info['action'] . $menuid;
                    }

                    $info['role_id'] = $roleid;

                    $info['status'] = $info['type'] ? 1 : 0;

                    $addauthorize[] = $info;
                }
                
                if (model('admin/Access')->batchAuthorize($addauthorize, $roleid)) {

                    $this->success("授权成功！", url("Rbac/rolemanage"));

                } else {

                    $error = model("admin/Access")->getError();

                    $this->error($error ? $error : '授权失败！');
                }
            } else {

                $this->error("没有接收到数据，执行清除授权成功！");
            }
        }

        //角色ID
        $roleid = input('id', 0, 'intval');

        if (empty($roleid)) {

            $this->error("参数错误！");
        }

        //菜单缓存
        $result = hk_cache("Menu");

        //获取已权限表数据
        $priv_data = model("admin/Role")->getAccessList($roleid);

        $json = array();

        foreach ($result as $rs) {

            $data = array(

                'id' => $rs['id'],

                'checked' => $rs['id'],

                'parentid' => $rs['parentid'],

                'name' => $rs['name'] . ($rs['type'] == 0 ? "(菜单项)" : ""),

                'checked' => model("admin/Role")->isCompetence($rs, $roleid, $priv_data) ? true : false,
            );

            $json[] = $data;
        }

        $this->assign('json', json_encode($json));

        $this->assign("roleid", $roleid);

        $this->assign('name', model("admin/Role")->getRoleIdName($roleid));

        return $this->fetch();
        
    }

    //栏目授权
    public function setting_cat_priv() {
        if ( request()->isPost() ) {
            $roleid = input('post.roleid', 0, 'intval');
            $priv = array();
            foreach ($_POST['priv'] as $k => $v) {
                foreach ($v as $e => $q) {
                    $priv[] = array("roleid" => $roleid, "catid" => $k, "action" => $q, "is_admin" => 1);
                }
            }
            //C('TOKEN_ON', false);
            //循环验证每天数据是否都合法
            $addpriv = [];
            foreach ($priv as $r) {
                $addpriv[] = $r;
            }
            //C('TOKEN_ON', true);
            //设置权限前，先删除原来旧的权限
            db("CategoryPriv")->where(array("roleid" => $roleid))->delete();
            //添加新的权限数据，使用D方法有操作记录产生
            db("CategoryPriv")->insertAll($addpriv);
            $this->success("权限赋予成功！");
        } else {
            $roleid = input('roleid', 0, 'intval');
            if(empty($roleid)){
                $this->error('请指定需要授权的角色！');
            }
            $categorys = hk_cache("Category");
            $tree = new Tree();
            $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
            $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
            $category_priv = db("CategoryPriv")->where(array("roleid" => $roleid))->select();
            $priv = array();
            foreach ($category_priv as $k => $v) {
                $priv[$v['catid']][$v['action']] = true;
            }

            foreach ($categorys as $k => $v) {
                $v = getCategory($v['catid']);
                if ($v['type'] == 1 || $v['child']) {
                    $v['disabled'] = 'disabled';
                    $v['init_check'] = '';
                    $v['add_check'] = '';
                    $v['edit_check'] = '';
                    $v['delete_check'] = '';
                    $v['listorder_check'] = '';
                    $v['push_check'] = '';
                    $v['move_check'] = '';
                } else {
                    $v['disabled'] = '';
                    $v['add_check'] = isset($priv[$v['catid']]['add']) ? 'checked' : '';
                    $v['delete_check'] = isset($priv[$v['catid']]['delete']) ? 'checked' : '';
                    $v['listorder_check'] = isset($priv[$v['catid']]['listorder']) ? 'checked' : '';
                    $v['push_check'] = isset($priv[$v['catid']]['push']) ? 'checked' : '';
                    $v['move_check'] = isset($priv[$v['catid']]['remove']) ? 'checked' : '';
                    $v['edit_check'] = isset($priv[$v['catid']]['edit']) ? 'checked' : '';
                }
                $v['init_check'] = isset($priv[$v['catid']]['init']) ? 'checked' : '';
                $categorys[$k] = $v;
            }
            $str = "<tr>
	<td align='center'><input type='checkbox'  value='1' onclick='select_all(\$catid, this)' ></td>
	<td>\$spacer\$catname</td>
	<td align='center'><input type='checkbox' name='priv[\$catid][]' \$init_check  value='init' ></td>
	<td align='center'><input type='checkbox' name='priv[\$catid][]' \$disabled \$add_check value='add' ></td>
	<td align='center'><input type='checkbox' name='priv[\$catid][]' \$disabled \$edit_check value='edit' ></td>
	<td align='center'><input type='checkbox' name='priv[\$catid][]' \$disabled \$delete_check  value='delete' ></td>
	<td align='center'><input type='checkbox' name='priv[\$catid][]' \$disabled \$listorder_check value='listorder' ></td>
	<td align='center'><input type='checkbox' name='priv[\$catid][]' \$disabled \$push_check value='push' ></td>
	<td align='center'><input type='checkbox' name='priv[\$catid][]' \$disabled \$move_check value='remove' ></td>
            </tr>";
            $tree->init($categorys);
            $categorydata = $tree->get_tree(0, $str);
            $this->assign("categorys", $categorydata);
            $this->assign("roleid", $roleid);
            return $this->fetch("categoryrbac");
        }
    }
}
